﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KVB.Common;
using Customer_Profile.eCard.DBDao;
using Customer_Profile.Admin;

namespace Customer_Profile.eCard.CardAccount
{
    public partial class ExpenseCheck : System.Web.UI.Page
    {
        string expense_no = "";
         
        string strUserName = "";
        string strUserID = "";
        string strEntity = "";
        public string strScript = "";
        public string card_balance = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            Comm.CheckSession(this.Page, "/login.aspx", "usermodel");
            if (Session["usermodel"] != null)
            {
                strUserID = ((UserInfoEntity)Session["usermodel"]).UserLoginId;
                strUserName = ((UserInfoEntity)Session["usermodel"]).UserName;
                strEntity = ((UserInfoEntity)Session["usermodel"]).UserOffice;
            }
            else
            {
                Log.WriteDefaultLog("main_01 Page_Load", "Session is null");
                return;
            }

            if (Request["expense_no"] != null)
            {
                expense_no = Request["expense_no"].ToString();
            }
            else
            {
                Response.Write("没有接收到expense-no");
                return;
            }

            //检查交易是否被锁定，如果没锁定，报错，请联系管理员，发短信给管理员
            if (!checkIfLocked(expense_no))
            {
                //2,发一条错误短信给系统管理员。
                Log.WriteDefaultLog(strUserID + " " + strEntity + " 未锁定交易，重复提交错误记录：", expense_no);                    
                lbWarning.Text = "Expense未锁定交易，或者已经处理过的交易，提交记录:"+expense_no+",请联系系统管理员。";
            }

            if (!Page.IsPostBack)
            {
                bindDetailInfo(int.Parse(expense_no));
            }
        }

        private void bindDetailInfo(int expense_no)
        {
            try
            {
                DataSet ds = AccountingDao.getDealAllInfo_byExpenseno(expense_no);
                //客户信息
                rptCparty.DataSource = ds.Tables[0].DefaultView;
                rptCparty.DataBind();
                //交易信息
                rptDeals.DataSource = ds.Tables[1].DefaultView;
                rptDeals.DataBind();
                //消费信息
                rptExpenseDeals.DataSource = ds.Tables[2].DefaultView;
                rptExpenseDeals.DataBind();
                //消费信息附件
                rptAttach.DataSource = ds.Tables[3].DefaultView;
                rptAttach.DataBind();
                //资金流
                gridCflows1.DataSource = ds.Tables[4].DefaultView;
                gridCflows1.DataBind();

                if (ds.Tables[5] != null && ds.Tables[5].Rows.Count > 0)
                {
                    card_balance = ds.Tables[5].Rows[0]["card_balance"].ToString();
                }

                if (ds.Tables[6] != null && ds.Tables[6].Rows.Count > 0)
                {
                    gridTransfer1.DataSource = ds.Tables[6].DefaultView;
                    gridTransfer1.DataBind();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

        protected void btnPass_Click(object sender, EventArgs e)
        {
            try
            {
                string status = "";
                string message = "";
                string pass_remark = txt_pass_remark.Text.Trim();

                AccountingDao.cardExpensePass(int.Parse(expense_no), pass_remark, strUserID, strUserName, out status, out message);
                if (status.Equals("Y"))
                {
                    strScript = "<script>alert('审核成功！'); window.opener.refresh();window.opener=null;window.close();</script>";
                }
                else
                {
                    lbWarning.Text = message;
                    strScript = "<script>alert('审核失败！'+" + message + "); </script>";
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("结算申请提交到交易总部出错了：", ex.ToString());
                Response.Write("系统出错了，请联系系统管理员!<br>" + ex.ToString());
            }
        }

        //检查交易是否被锁定，如果没有被锁定，是有问题的。
        private bool checkIfLocked(string ref_expense_no)
        {
            bool if_locked = false;

            DataSet ds = DealDao.getIfLocked_byExpenseno(ref_expense_no);
            if (!Comm.DataSetIsEmpty(ds))
            {
                string if_slocked = ds.Tables[0].Rows[0][0].ToString();
                if (if_slocked.ToUpper().Trim().Equals("Y"))
                    if_locked = true;
                else
                    if_locked = false;
            }
            else
            {
                Log.WriteDefaultLog("checkIfLocked-Expense:" + ref_expense_no + "\r\n", "ds不能为空，如果为空那里出错了");
            }

            return if_locked;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            try
            {
                string status = "";
                string message = "";
                string back_remark = txt_back_remark.Text.Trim();

                AccountingDao.cardExpenseBack(int.Parse(expense_no), back_remark, strUserID, strUserName, out status, out message);
                if (status.Equals("Y"))
                {
                    strScript = "<script>alert('审核退回成功！'); window.opener.refresh();window.opener=null;window.close();</script>";
                }
                else
                {
                    lbWarning.Text = message;
                    strScript = "<script>alert('审核失败！'+" + message + "); </script>";
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("结算申请提交到交易总部出错了：", ex.ToString());
                Response.Write("系统出错了，请联系系统管理员!<br>" + ex.ToString());
            }
        }
    }
}